﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using Sale_Operation;
using Sale_Common;
using Sale_Model;
using SaleSystem;
using ExtAspNet;
namespace SaleSystem.Other
{
    public partial class frm_TotalEdit : PageBase
    {
        #region Page Event
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                InitControl();
                GetAndBind();
            }
        }
        protected void bnSave_Click(object sender, EventArgs e)
        {
           //更新结算方式
            switch (this.Type)
            {
                case 1:
                    BuyBillOp Manager1 = new BuyBillOp();
                    Sale_Model.BuyBill SModel1 = new Sale_Model.BuyBill();
                    SModel1.BBill_Id = this.Id;
                    Manager1.GetModel(SModel1);
                    SModel1.BBilll_PayType = ValueHandler.GetIntNumberValue(this.ddl_BBilll_PayType.SelectedValue);
                    SModel1.BBill_AList_Id = ValueHandler.GetIntNumberValue(this.ddl_BBill_AList_Id.SelectedValue);
                    Manager1.Update(SModel1);
                    break;
                case 2:
                    ClientBackBillOp Manager2 = new ClientBackBillOp();
                    Sale_Model.ClientBackBill SModel2 = new Sale_Model.ClientBackBill();
                    SModel2.CBB_Id = this.Id;
                    Manager2.GetModel(SModel2);
                    SModel2.CBB_PayType = ValueHandler.GetIntNumberValue(this.ddl_BBilll_PayType.SelectedValue);
                    SModel2.CBB_AList_Id = ValueHandler.GetIntNumberValue(this.ddl_BBill_AList_Id.SelectedValue);
                    Manager2.Save(SModel2);
                    break;
                case 3:
                    MyBackBillOp Manager3 = new MyBackBillOp();
                    Sale_Model.MyBackBill SModel3 = new Sale_Model.MyBackBill();
                    SModel3.MBB_Id = this.Id;
                    Manager3.GetModel(SModel3);
                    SModel3.MBB_PayType = ValueHandler.GetIntNumberValue(this.ddl_BBilll_PayType.SelectedValue);
                    SModel3.MBB_AList_Id = ValueHandler.GetIntNumberValue(this.ddl_BBill_AList_Id.SelectedValue);
                    Manager3.Save(SModel3);
                    break;
            } 
           PageContext.RegisterStartupScript(  ExtAspNet.ActiveWindow.GetHidePostBackReference("bnTotal"));
        }

        protected void ddl_SBill_PayTypeSelectedChanged(object obj, EventArgs e)
        {
            SysConfig SM = new SysConfig();
            if (this.ddl_BBilll_PayType.SelectedValue == "1")//现金
            {
                SM.SConf_Id = 4;
            }
            else
                SM.SConf_Id = 5;
            SysConfigOp op = new SysConfigOp();
            op.GetConfig(SM);
            this.ddl_BBill_AList_Id.SelectedValue = SM.SConf_Value;
        }
        #endregion

        #region Method
        private void InitControl()
        {
            bnClose.OnClientClick = ExtAspNet.ActiveWindow.GetHideReference();
            if (Request["Id"] != null)
            {
                Id = ValueHandler.GetIntNumberValue(Request["Id"]);
            }
            if (Request["Type"] != null)
            {
                Type = ValueHandler.GetIntNumberValue(Request["Type"]);
            }
            //结算方式

            this.ddl_BBilll_PayType.DataSource = LinkService.GetPayType();
            this.ddl_BBilll_PayType.DataBind();
            this.ddl_BBilll_PayType.Items.Insert(0, new ExtAspNet.ListItem("-未选择-", "-1"));
            //结算状态
            //this.ddl_SBill_State.DataSource = LinkService.GetSBill_State();
            //this.ddl_SBill_State.DataBind();
            //this.ddl_SBill_State.Items.Insert(0, new ExtAspNet.ListItem("-未选择-", "-1"));
            //账户
            AccountListOp AccountManager = new AccountListOp();
            DataTable dtAccountList = AccountManager.GetList("");
            this.ddl_BBill_AList_Id.DataSource = dtAccountList;
            this.ddl_BBill_AList_Id.DataBind();
            this.ddl_BBill_AList_Id.Items.Insert(0, new ExtAspNet.ListItem("-未选择-", "-1"));


        }
        private void GetAndBind()
        {
            switch (this.Type)
            {
                case 1:
                    BuyBillOp Manager1 = new BuyBillOp();
                    Sale_Model.BuyBill SModel1 = new Sale_Model.BuyBill();
                    SModel1.BBill_Id = this.Id;
                    Manager1.GetModel(SModel1);
                    lb_Money.Text = SModel1.BBill_Money.ToString();
                    this.ddl_BBilll_PayType.SelectedValue = SModel1.BBilll_PayType.ToString();
                    this.ddl_BBill_AList_Id.SelectedValue = SModel1.BBill_AList_Id.ToString();
                    break;
                case 2:
                    ClientBackBillOp Manager2 = new ClientBackBillOp();
                    Sale_Model.ClientBackBill SModel2 = new Sale_Model.ClientBackBill();
                    SModel2.CBB_Id = this.Id;
                    Manager2.GetModel(SModel2);
                    lb_Money.Text = SModel2.CBB_Money.ToString();

                    this.ddl_BBilll_PayType.SelectedValue = SModel2.CBB_PayType.ToString();
                    this.ddl_BBill_AList_Id.SelectedValue = SModel2.CBB_AList_Id.ToString();
                    break;
                case 3:
                    MyBackBillOp Manager3 = new MyBackBillOp();
                    Sale_Model.MyBackBill SModel3 = new Sale_Model.MyBackBill();
                    SModel3.MBB_Id = this.Id;
                    Manager3.GetModel(SModel3);
                    lb_Money.Text = SModel3.MBB_Money.ToString();
                    this.ddl_BBilll_PayType.SelectedValue = SModel3.MBB_PayType.ToString();
                    this.ddl_BBill_AList_Id.SelectedValue = SModel3.MBB_AList_Id.ToString();
                    break;
            }
        }

        #endregion

        #region Property
        /// <summary>
        /// 1:进货单结算确认 2：退货单结算确认 3：返货单结算确认
        /// </summary>
        private int Type
        {
            get
            {
                return ValueHandler.GetIntNumberValue(ViewState["_Type"]);
            }
            set
            {
                ViewState["_Type"] = value;
            }
        }
        #endregion
    }
}
